cmake_minimum_required(VERSION 3.22)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
project(MSPM0G3507 LANGUAGES C CXX ASM)

set(CMAKE_EXECUTABLE_SUFFIX_ASM     ".out")
set(CMAKE_EXECUTABLE_SUFFIX_C       ".out")
set(CMAKE_EXECUTABLE_SUFFIX_CXX     ".out")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")  # 强制使用 .a 后缀
# 显式设置库后缀优先级，但没有任何作用
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;${CMAKE_FIND_LIBRARY_SUFFIXES}")

# =============================== 解决ti-clang觉得rsp文件太长 =============================
# ========= TI 编译器响应文件格式修复 =========
# 设置响应文件格式为每行一个参数
set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_ASM_RESPONSE_FILE_LINK_FLAG "@")

# 强制 CMake 在响应文件中每行一个参数
set(CMAKE_C_RESPONSE_FILE_FORMAT "LINES")
set(CMAKE_CXX_RESPONSE_FILE_FORMAT "LINES")
set(CMAKE_ASM_RESPONSE_FILE_FORMAT "LINES")

# 对于 Ninja 生成器，需要额外设置
if(CMAKE_GENERATOR MATCHES "Ninja")
    set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)
    set(CMAKE_NINJA_RESPONSE_FILE_CONTENT_FORMAT "LINES")
    message(STATUS "Enabled per-line response files for TI compiler (Ninja)")
else()
    message(STATUS "Enabled per-line response files for TI compiler")
endif()
# ============================================

# ===============================  目录 =============================
# 定义可配置的 SDK 路径变量（带默认值和描述）
set(SDK_DIR "D:/DevTools/TI/CCS2020/mspm0_sdk_2_05_01_00" CACHE PATH "Path to TI MSPM0 SDK")
message(STATUS "Using SDK path: ${SDK_DIR}")

# 检查 SDK 路径是否有效
if(NOT EXISTS ${SDK_DIR})
    # 在CLion的CMake配置选项里传递参数【-DSDK_DIR="……"】，引号内就是路径
    message(SEND_ERROR "SDK directory not found: ${SDK_DIR}")
endif()

# 获取编译器所在目录（bin目录）
get_filename_component(COMPILER_DIR "${CMAKE_C_COMPILER}" DIRECTORY)
message(STATUS "Compiler bin directory: ${COMPILER_DIR}")

# 获取上级目录（编译器根目录）
get_filename_component(COMPILER_ROOT_DIR "${COMPILER_DIR}" DIRECTORY)
message(STATUS "Compiler root directory: ${COMPILER_ROOT_DIR}")

# =============================== 编译链接 =================================
# 添加预编译宏
add_compile_definitions(
        __MSPM0G3507__
)
# 添加编译标志
add_compile_options(
        -march=thumbv6m -mcpu=cortex-m0plus -mfloat-abi=soft -mlittle-endian -mthumb -O0
        -gdwarf-3

)
# 添加链接标志
add_link_options(
        -Wl,-mmspm0g3507.map
        -Wl,--diag_wrap=off -Wl,--display_error_number
        -Wl,--warn_sections -Wl,--xml_link_info=${CMAKE_SOURCE_DIR}/Debug/mspm0g3507_linkInfo.xml -Wl,--rom_model
        # 添加链接脚本
        -Wl,-l${CMAKE_SOURCE_DIR}/Debug/device_linker.cmd
)
# 创建可执行目标
add_executable(${PROJECT_NAME})


# ----------- 添加链接库目录 -----------
target_link_directories(${PROJECT_NAME} PUBLIC
        ${CMAKE_SOURCE_DIR}/Debug/syscfg
        ${COMPILER_ROOT_DIR}/lib
)

# ----------- 添加头文件目录 -----------
target_include_directories(${PROJECT_NAME} PUBLIC
        # 默认目录
        ${SDK_DIR}/source
        ${SDK_DIR}/source/third_party/CMSIS/Core/Include
        ${CMAKE_SOURCE_DIR}
        Debug

        # 用户自定义目录
        bsp
        core
        hal
)



# ----------- 添加源文件 -----------
file(GLOB_RECURSE SOURCE
        # 默认文件
        "${SDK_DIR}/source/ti/devices/msp/m0p/startup_system_files/ticlang/startup_mspm0g350x_ticlang.c"
        # 驱动库文件，不能直接在SDK目录里以静态库的方式链接，否则会出问题。TI编译器过于特立独行，寻常CMake链接库的实现根本就不能使用
        "${SDK_DIR}/source/ti/driverlib/*.c"
        "Debug/ti_msp_dl_config.c"

        # 用户自定义文件
        "core/*.cpp"

)

# 添加资源文件
target_sources(${PROJECT_NAME} PUBLIC ${SOURCE})



# ================== 创建 LVGL 静态库 ==================
add_library(lvgl STATIC)
file(GLOB_RECURSE LVGL_SOURCES "middleware/LVGL/lvgl/src/*.c")
target_sources(lvgl PUBLIC ${LVGL_SOURCES})
target_include_directories(lvgl PUBLIC
        ${CMAKE_SOURCE_DIR}/middleware/LVGL/lvgl
        ${CMAKE_SOURCE_DIR}/middleware/LVGL/lvgl/src
)
# 设置静态库输出目录
set_target_properties(lvgl PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/middleware/lib
)


# ================== DSP库 ==================
set(DSP_LIB_DIR ${SDK_DIR}/source/third_party/CMSIS/DSP/lib/ticlang/m0p)
set(DSP_INC_DIR ${SDK_DIR}/source/third_party/CMSIS/DSP/Include)

# ============= 为可执行目标添加静态库 ===============
# 链接库目录
target_include_directories(${PROJECT_NAME} PUBLIC
     ${DSP_LIB_DIR}
)
# 链接静态库
target_link_libraries(${PROJECT_NAME} PUBLIC
        lvgl
        # 全名以应对默认查找.lib的混账问题
        ${DSP_LIB_DIR}/arm_cortexM0l_math.a
)

if (NOT EXISTS ${DSP_INC_DIR})
    message(SEND_ERROR "INC:${DSP_INC_DIR}")
endif ()
# 头文件目录
target_include_directories(${PROJECT_NAME} PUBLIC
        ${DSP_INC_DIR}
)

